// TypeScript
// import { v2 as webdav } from "webdav-server";
// Javascript
const webdav = require("webdav-server").v2;
const express = require("express");
const fs = require("fs");
const path = require("path");

// User manager (tells who are the users)
const userManager = new webdav.SimpleUserManager();
const user = userManager.addUser("username", "password", false);

// Privilege manager (tells which users can access which files/folders)
const privilegeManager = new webdav.SimplePathPrivilegeManager();
privilegeManager.setRights(user, "/", ["all"]);

const storagePath = path.resolve(process.cwd(), "data"); // 存储目录
console.log("storage path :" + storagePath);
if (!fs.existsSync(storagePath)) {
  fs.mkdirSync(storagePath, { recursive: true });
}

const server = new webdav.WebDAVServer({
  // HTTP Digest authentication with the realm 'Default realm'
  httpAuthentication: new webdav.HTTPDigestAuthentication(
    userManager,
    "Default realm"
  ),
  // privilegeManager: privilegeManager,
  port: 1900, // Load the server on the port 2000 (if not specified, default is 1900)
  // autoSave: {
  //   // Will automatically save the changes in the 'data.json' file
  //   treeFilePath: "data.json",
  // },

  rootFileSystem: new webdav.PhysicalFileSystem(storagePath),
});

// Try to load the 'data.json' file
server.autoLoad((e) => {
  if (e) {
    // Couldn't load the 'data.json' (file is not accessible or it has invalid content)
    server.rootFileSystem().addSubTree(server.createExternalContext(), {
      folder1: {
        // /folder1
        "file1.txt": webdav.ResourceType.File, // /folder1/file1.txt
        "file2.txt": webdav.ResourceType.File, // /folder1/file2.txt
      },
      "file0.txt": webdav.ResourceType.File, // /file0.txt
    });
  }

  const app = express();
  // Mount the WebDAVServer instance
  app.use(webdav.extensions.express("/kkk", server));
  app.listen(1900); // Start the Express server

  // server.start(() => console.log("READY"));
  console.log("READY");
});
